package com.cy.embeddingdemo.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
public class RedisUtil {


    @Autowired
    private StringRedisTemplate redisTemplate;

    public void putTask(String taskId) {
        redisTemplate.opsForHash().put("processing_tasks", taskId, "processing");
        System.out.println("Task added to Redis: " + taskId);
    }

    public boolean isTaskProcessing(String taskId) {
        String status = (String) redisTemplate.opsForHash().get("processing_tasks", taskId);
        return "processing".equals(status);
    }

    public void removeTask(String taskId) {
        redisTemplate.opsForHash().delete("processing_tasks", taskId);
        System.out.println("Task removed from Redis: " + taskId);
    }

    public Iterable<String> getAllTaskIds() {
        Set<Object> keys = redisTemplate.opsForHash().keys("processing_tasks");
        return keys.stream().map(Object::toString).toList();
    }

}
